

package com.lzb.compressmaster.dialog;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.basic.BaseSmartDialog;
import com.lzb.compressmaster.databinding.DialogRenameBinding;
import com.lzb.compressmaster.databinding.DialogWatermarkBinding;

public class WatermarkDialog extends BaseSmartDialog<DialogWatermarkBinding> implements View.OnClickListener {

    public WatermarkDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_watermark;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);
        mDataBinding.ivClear.setOnClickListener(this);

        mDataBinding.etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                if (TextUtils.isEmpty(str)) {
                    mDataBinding.ivClear.setVisibility(View.INVISIBLE);
                } else {
                    mDataBinding.ivClear.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            dismiss();
            if (!TextUtils.isEmpty(mDataBinding.etName.getText().toString().trim())) {
                if (listener != null) {
                    listener.clickConfirm(mDataBinding.etName.getText().toString());
                }
            } else {
                ToastUtils.showShort(R.string.please_input_watermark);
            }
        } else if (id == R.id.ivClear) {
            mDataBinding.etName.setText("");
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm(String name);
    }
}
